package cn.itsource.controller;

import cn.itsource.constants.RConst;
import cn.itsource.result.JSONResult;
import cn.itsource.service.EmailTemplate;
import cn.itsource.service.TXCosTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @program fccar_240420
 * @Description
 * @Date 2024/09/12 16:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private TXCosTemplate txCosTemplate;
    @Autowired
    private EmailTemplate emailTemplate;
    //上传文件到COS云服务器
    @PostMapping("/cos/upload")
    public JSONResult uploadCos(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//这个方法获取的是上传文件的文件名  xxx.png
        int i = originalFilename.lastIndexOf(RConst.Str.DOT);
        String suffix = originalFilename.substring(i, originalFilename.length());
        String imgName = UUID.randomUUID().toString();
        emailTemplate.simpleSendEmail("321840845@qq.com","382279894@qq.com",
                "测试邮箱发送","<h1>哈哈哈哈红红火火恍恍惚惚</h1>");
        return JSONResult.success(txCosTemplate.upload(file.getInputStream(), imgName+suffix));
    }
}
